/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.skuboard.session;

import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import org.skuboard.entity.Users;
import org.skuboard.model.Student;

/**
 *
 * @author clockbuilder
 */
@Stateless
public class UsersFacade extends AbstractFacade<Users> {

    @PersistenceContext(unitName = "www.skuboard.orgPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsersFacade() {
        super(Users.class);
    }

    public List<Student> findByUserType(String userType) {
        List<Student> listOfAllStudents = null;
        List<Users> listOfAllUsersEntity = null;
        Query queryUsersByUserType = (Query) em.createNamedQuery("findByUserType");
        queryUsersByUserType.setParameter("userType", userType);
        listOfAllUsersEntity = queryUsersByUserType.getResultList();
        Iterator<Users> iterator = listOfAllUsersEntity.iterator();
        if (iterator.hasNext()) {
            Users studentEntity = iterator.next();
            String Id = studentEntity.getUsersPK().getId();
            String firstName = studentEntity.getFirstName();
            String lastName = studentEntity.getLastName();
            String otherName = studentEntity.getOtherNames();
            String dateOfBirth = studentEntity.getDob();
            String address = studentEntity.getAddress();
            String eMail = studentEntity.getEmail();
            Student student = new Student(userType, firstName, lastName, otherName, dateOfBirth, address, eMail);
            listOfAllStudents.add(student);
        }
        return listOfAllStudents;
    }

    public Student findStudentByID(String studentId) {
        //Ideally, this should run an sql that queries for (User WHERE id=studentID and userType="student")
        Student student = new Student();
//        UsersPK usersPK = new UsersPK(studentId, "student");
//        Users studentEntity = em.find(Users.class, usersPK);


        Users studentEntity = em.createQuery("SELECT u FROM Users u WHERE u.usersPK.id = :id", Users.class)
                .getSingleResult();
//        String queryName = "findById";
//        Users studentEntity = em.createNamedQuery(queryName, Users.class)
//                                     .setParameter("id", studentId)
//                                     .getSingleResult();


        String Id = studentEntity.getUsersPK().getId();
        String firstName = studentEntity.getFirstName();
        String lastName = studentEntity.getLastName();
        String otherName = studentEntity.getOtherNames();
        String dateOfBirth = studentEntity.getDob();
        String address = studentEntity.getAddress();
        String eMail = studentEntity.getEmail();
        student = new Student(Id, firstName, lastName, otherName, dateOfBirth, address, eMail);
        return student;
    }
}
